package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.core.SeckillKey;
import com.dongdongshop.mapper.TbSeckillGoodsMapper;
import com.dongdongshop.mapper.TbSeckillOrderMapper;
import com.dongdongshop.pojo.TbSeckillGoods;
import com.dongdongshop.pojo.TbSeckillOrder;
import com.dongdongshop.service.SeckillOrderService;
import com.dongdongshop.util.IdWorker;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
@Component
public class SeckillOrderServiceImpl implements SeckillOrderService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Resource
    private TbSeckillGoodsMapper seckillGoodsMapper;
    @Resource
    private TbSeckillOrderMapper seckillOrderMapper;
    //注入工具类
    @Autowired
    private IdWorker idWorker;
    @Resource
    private RocketMQTemplate rocketMQTemplate;


    @Override
    public void submitOrder(Long seckillId, Long id) {
        RedisSerializer redisSerializer = new StringRedisSerializer();
        redisTemplate.setValueSerializer(redisSerializer);
        //redisTemplate.setKeySerializer();
        //从缓存中获取秒杀的商品的库存
        Integer goodsStockCount = Integer.parseInt(redisTemplate.boundValueOps(seckillId).get().toString()) ;

        //如果库存不存在
        if (null == goodsStockCount){
            //抛出异常告诉调用方
            throw  new RuntimeException("商品不存在");
        }
        //商品库存为0
        if (goodsStockCount <= 0){
            throw  new RuntimeException("商品卖完了");
        }
        //扣减库存
        Long stockCount = redisTemplate.boundValueOps(seckillId).decrement();

        //同步商品缓存
        TbSeckillGoods goods = (TbSeckillGoods)redisTemplate.boundHashOps(SeckillKey.SECKILLGOODS_KEY).get(seckillId);
        goods.setStockCount(stockCount.intValue());
        redisTemplate.boundHashOps(SeckillKey.SECKILLGOODS_KEY).put(seckillId,goods);
        //如果已经是最后一件商品
        if (stockCount == 0){//商品被抢光
            TbSeckillGoods seckillGoods = (TbSeckillGoods)redisTemplate.boundHashOps(SeckillKey.SECKILLGOODS_KEY).get(seckillId);
            seckillGoods.setStockCount(0);
            //同步数据库
            seckillGoodsMapper.updateByPrimaryKeySelective(seckillGoods);
            //如果卖完了，删除缓存
            redisTemplate.boundHashOps(SeckillKey.SECKILLGOODS_KEY).delete(seckillId);
            //删除库存
            redisTemplate.delete(seckillId);
        }
        //保存订单
        Map<String,Long> msg = new HashMap<>();
        msg.put("seckillId",seckillId);
        msg.put("userId",id);
        rocketMQTemplate.convertAndSend("SECKILLGOODS",msg);


    }

    @Override
    public TbSeckillOrder findOrderFromRedis(Long seckillId, Long id) {
        return (TbSeckillOrder) redisTemplate.boundHashOps(SeckillKey.SECKILLORDERS_KEY).get(id+"_"+seckillId);
    }
}
